package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.acticle.domain.QuestionAndAnswer;
import cn.wolfcode.luowowo.acticle.domain.QuestionAndAnswerComment;
import cn.wolfcode.luowowo.acticle.domain.QuestionAndAnswerContent;
import cn.wolfcode.luowowo.acticle.query.QuestionAndAnswerQuery;
import cn.wolfcode.luowowo.acticle.service.IQuestionAndAnswerCommentService;
import cn.wolfcode.luowowo.acticle.service.IQuestionAndAnswerContentService;
import cn.wolfcode.luowowo.acticle.service.IQuestionAndAnswerService;
import cn.wolfcode.luowowo.acticle.service.IRegionService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/qanda")
public class QAndAController {

    @Value("${file.path}")
    private String filePath;
    @Reference
    private IRegionService regionService;

    @Reference
    private IQuestionAndAnswerService questionAndAnswerService;

    @Reference
    private IQuestionAndAnswerContentService questionAndAnswerContentService;

    @Reference
    private IQuestionAndAnswerCommentService questionAndAnswerCommentService;




    @RequestMapping("")
    public String list(Model model, @ModelAttribute("qo")QuestionAndAnswerQuery qo){
        PageInfo pageInfo = questionAndAnswerService.query2(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "qanda/list";
    }

    @RequestMapping("input")
    public String input(Model m, Long id) {
        List<Map<String, Object>> destByDeep = regionService.getDestByDeep(3);
        m.addAttribute("regions", destByDeep);
        if (id != null) {
            QuestionAndAnswerContent questionAndAnswerContent = questionAndAnswerContentService.get(id);
            QuestionAndAnswer questionAndAnswer = questionAndAnswerService.get(id);
            questionAndAnswer.setQuestionAndAnswerContent(questionAndAnswerContent);

            m.addAttribute("qaa", questionAndAnswer);
        }
        return "qanda/input";
    }

    @RequireLogin
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(QuestionAndAnswer questionAndAnswer, @UserParam UserInfo userInfo) {
        questionAndAnswer.setAuthor(userInfo);
        Long id = questionAndAnswerService.saveOrUpdate(questionAndAnswer);

        return new AjaxResult().addData(id);
    }

    @RequestMapping("detail")
    public String detail(Model model , Long id, @UserParam UserInfo userInfo,@ModelAttribute("qo")QuestionAndAnswerQuery qo){
        QuestionAndAnswer detail = questionAndAnswerService.get(id);
        QuestionAndAnswerContent content = questionAndAnswerContentService.get(id);
        List<QuestionAndAnswerComment> firstLevelList = questionAndAnswerCommentService.queryFirstLevelList(id);
        model.addAttribute("firstLevelList",firstLevelList);
        detail.setQuestionAndAnswerContent(content);
        model.addAttribute("detail",detail);

        return "qanda/detail";
    }

    //上传图片
    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public Object coverImageUpload(MultipartFile pic) {
        String upload = UploadUtil.upload(pic, filePath);
        return upload;
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, filePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    //用户添加评论
    @RequireLogin
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(@UserParam UserInfo userInfo,QuestionAndAnswerComment comment,Long toId,Long replyId) {
        comment.setFromUser(userInfo);
        questionAndAnswerCommentService.save(comment,toId,replyId);
        return new AjaxResult().addData(comment);
    }


}
